package com.ssx.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.ssx.entity.Employees;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ssx.entity.Role;
import org.apache.ibatis.annotations.*;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author ssx
 * @since 2020-10-24
 */
public interface EmployeesMapper extends BaseMapper<Employees> {
    @Update("update employees set password=123456 where empid=#{id}")
    int resetPassord(int id);


    @Select("SELECT employees.*,roleInfo,roleName from employees LEFT JOIN role ON employees.roleId=role.roleId and employees.roleId>1 ${ew.customSqlSegment}")
    @Results({
            @Result(column = "roleId",
                    property = "role",
                    javaType = Role.class,
                    one = @One(select = "com.ssx.mapper.RoleMapper.selectById")
            )
    })
    public List<Employees> selelctPageByCondition(@Param(Constants.WRAPPER) Wrapper wrapper);

    @Select("SELECT employees.*,roleInfo,roleName from employees LEFT JOIN role ON employees.roleId=role.roleId and employees.roleId>2 ${ew.customSqlSegment}")
    @Results({
            @Result(column = "roleId",
                    property = "role",
                    javaType = Role.class,
                    one = @One(select = "com.ssx.mapper.RoleMapper.selectById")
            )
    })
    public List<Employees> selelctPageByConditionJsutEmp(@Param(Constants.WRAPPER) Wrapper wrapper);

    @Select("select count(*) from employees LEFT JOIN role ON employees.roleId=role.roleId and employees.roleId>1 ${ew.customSqlSegment}")
    public int selelctPageCount(@Param(Constants.WRAPPER) Wrapper wrapper);

    @Select("select count(*) from employees LEFT JOIN role ON employees.roleId=role.roleId and employees.roleId>2 ${ew.customSqlSegment}")
    public int selelctPageCountJsutEmp(@Param(Constants.WRAPPER) Wrapper wrapper);

    @Select("SELECT employees.*,roleInfo,roleName from employees LEFT JOIN role ON employees.roleId=role.roleId where empid=#{id}")
    @Results({
            @Result(column = "roleId",
                    property = "role",
                    javaType = Role.class,
                    one = @One(select = "com.ssx.mapper.RoleMapper.selectById")
            )
    })
    @Override
    Employees selectById(Serializable id);

    @Select("SELECT employees.*,roleInfo,roleName from employees LEFT JOIN role ON employees.roleId=role.roleId ${ew.customSqlSegment}")
    @Results({
            @Result(column = "roleId",
                    property = "role",
                    javaType = Role.class,
                    one = @One(select = "com.ssx.mapper.RoleMapper.selectById")
            )
    })
    @Override
    Employees selectOne(@Param(Constants.WRAPPER) Wrapper<Employees> queryWrapper);

    @Insert("insert into employees (username,password,tel,name,email,roleId)VALUES(#{username},#{password},#{tel},#{name},#{email},3)")
    @Override
    int insert(Employees entity);

    @Update("update employees set username=#{username},password=#{password},tel=#{tel},name=#{name},email=#{email},roleId=#{role.roleId} where empid=#{empid}")
    @Override
    int updateById(Employees entity);
}
